import React, { PureComponent } from 'react';
import { connect } from 'dva';
import PropTypes from 'prop-types';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { PrimaryC, PrimaryGhostC, FixedButtonC } from '../../../../comps';
import styles from './index.less';
import BasicInfo from '../../../Product/moneyInformation/basicInfo'; //赎回计息
import FormContent from '../../../Product/nonstandarmaintenanceNew/FormContent';
import CardC from '../../Barra/comps/cardC';
@Form.create()
@connect(({ loading, informationM, moneyInformationM }) => ({ loading, informationM, moneyInformationM }))
export default class UpdataForm extends PureComponent {
  state = {
    baseBaseInfoChangeReport: {
      // 报表信息
      parTotalValue: undefined, // 发行价格
      interestMethod: undefined, // 计息方式
      interestRate: undefined, // 计息频率
      interestBasis: undefined, // 计息基础
      startDate: undefined, // 起息日
      endDate: undefined, // 到期日
      fixedRate: undefined, // 票面利率
    },
  };
  componentDidMount() {
    const {
      securityType,
      pageState,
      informationM: { basicInfo },
    } = this.props;
    if (securityType == '7' && pageState == '5') {
      this.setState({
        baseBaseInfoChangeReport: {
          // 发行价格
          parTotalValue: basicInfo.issueprice,
          // 计息方式
          interestMethod: basicInfo.twocycle,
          // 计息频率
          interestRate: basicInfo.interestFrequency,
          // 计息基础
          interestBasis: basicInfo.interestRules,
          // 起息日
          startDate: basicInfo.interestDate,
          // 到期日
          endDate: basicInfo.cashDueDate,
          // 票面利率
          fixedRate: basicInfo.couponRate,
        },
      });
    }
  }
  UNSAFE_componentWillReceiveProps(nextProps) {}

  getBaseInfo = () => {
    const { record } = this.props;
    if (this.props.securityType == '12') {
      // 非理财直融
      this.props.dispatch({
        type: 'informationM/getDataById',
        payload: {
          tradingcode: record?.stockCode ? record?.stockCode.split('.')[0] : undefined,
        },
      });
    } else {
      // 理财直融
      this.props.dispatch({
        type: 'informationM/editInformationById',
        payload: {
          tradingcode: record?.stockCode ? record?.stockCode.split('.')[0] : undefined,
          exchangecode: record?.exchangeCode,
        },
      });
    }
  };

  // 理财直融编辑新增
  confirmOk = () => {
    const {
      pageState,
      informationM: { basicInfo },
    } = this.props;
    let formdata = this.basicInfoRef.getInfo();
    if (formdata) {
      let redempArrs = this.basicInfoRef.redempsGetInfo();
      if (!redempArrs) return;
      let params = {
        ...formdata,
        id: pageState == '5' ? basicInfo.id : '',
        type: pageState == '4' ? 'add' : 'update',
        zgtzptNonstandarCashFlows: redempArrs,
      };
      this.props.dispatch({
        type: 'moneyInformationM/addMoneyInformation',
        payload: params,
        callback: (data) => {
          if (data) {
            if (pageState == '5') this.getBaseInfo();
            this.props.changePage(1);
          }
        },
      });
    }
  };

  render() {
    const {
      form,
      securityType,
      pageState,
      informationM: { basicInfo, formDatas },
    } = this.props;
    let formDatasItem = {
      // 非理财直融
      baseInfo: {}, //基本信息
      reportInfo: {}, //监管报表信息
      files: [], //资产包
      zgtzptNonstandarCashFlows: [], //赎回计息
    };

    return (
      <div className={styles.updataForm}>
        <CardC title={pageState == '4' ? '新增' : '编辑'} type='back' showContent={true} titleIsShow={false} changeFormPage={(e) => this.props.changePage(1)} />
        {/* 内容 */}
        <div className={styles.baseInfo}>
          {securityType == '7' ? (
            <div>
              <BasicInfo
                form={form}
                init={pageState == '4' ? {} : basicInfo}
                type={pageState == '4' ? 1 : 2}
                onCancel={() => this.props.changePage(1)}
                onSave={() => this.confirmOk()}
                onRef={(ref) => (this.basicInfoRef = ref)}
              />
            </div>
          ) : (
            <FormContent
              type={'workStation'}
              formDatas={pageState == '4' ? formDatasItem : formDatas}
              activeKey={1}
              pageType={pageState == '4' ? 'add' : 'edit'}
              saveReshList={() => {
                if (pageState == '5') this.getBaseInfo();
                this.props.changePage(1);
              }}
              CancelFn={() => this.props.changePage(1)}
            ></FormContent>
          )}
        </div>
      </div>
    );
  }
}

// props默认值
UpdataForm.propsTypes = {
  securityType: PropTypes.string, // 标的类型：7-非标(理财直融) 12- 非标(非理财直融)）
  securityCode: PropTypes.string, // 标的code
  pageState: PropTypes.string, // 4-新增/编辑非标理财直融  5-新增/编辑非标非理财直融
};
